package com.myopen.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.myopen.converter.ListConverter;
import com.myopen.converter.ObjectConverter;
import com.myopen.kit.UUIDKit;

public class WebsiteContent extends Model<WebsiteContent>{

	private static final long serialVersionUID = 3180656849023425191L;
	public static final WebsiteContent dao = new WebsiteContent();
	
	public Page<Record> pagequery(Record record){
		StringBuffer sqlExceptSelect=new StringBuffer("from t_website_content t "
				+ " left join t_website w on t.websiteId=w.id"
				+ " left join t_website_column c on t.websiteColumnId=c.id "
				+ " where 1=1 ");
		List<Object> list=new ArrayList<Object>();
		
		
		if(StrKit.notBlank(record.getStr("removed"))){
			sqlExceptSelect.append(" and t.removed = ? ");
			list.add(record.getStr("removed"));
		}else {
			sqlExceptSelect.append(" and t.removed='0' ");
		}
		
		if(StrKit.notBlank(record.getStr("websiteId"))){
			sqlExceptSelect.append(" and t.websiteId = ? ");
			list.add(record.getStr("websiteId"));
		}
		
		if(StrKit.notBlank(record.getStr("code"))){
			sqlExceptSelect.append(" and c.code = ? ");
			list.add(record.getStr("code"));
		}
		
		if(StrKit.notBlank(record.getStr("websiteColumnId"))){
			sqlExceptSelect.append(" and t.websiteColumnId = ? ");
			list.add(record.getStr("websiteColumnId"));
		}
		
		if(StrKit.notBlank(record.getStr("module"))){
			sqlExceptSelect.append(" and c.module = ? ");
			list.add(record.getStr("module"));
		}
		
		if(StrKit.notBlank(record.getStr("searchContent"))){
			sqlExceptSelect.append(" and (t.title like ?) ");
			list.add("%"+record.getStr("searchContent")+"%");
		}
		
		if(StrKit.notBlank(record.getStr("status"))){
			sqlExceptSelect.append(" and t.status =? ");
			list.add(record.getStr("status"));
		}
		
		sqlExceptSelect.append(" order by t.createTime desc");
		Page<Record> page= Db.paginate(record.getInt("pageNumber"),record.getInt("pageSize"),"select t.*,w.name websiteName,c.name websiteColumnName,c.code",sqlExceptSelect.toString(),list.toArray());		
		String[] dicTypeKeys= {"module","status"};
		String[] dicTypes= {"CMS_MODULE","CMS_CONTENT_STATUS"};
		String[] mapTypes= {"attribute"};
		
		return new ListConverter<Record>(page,dicTypeKeys,dicTypes,mapTypes).getPage();
	}
	
	public void save(WebsiteContent vo,Record currUser) {
		WebsiteContent bo=null;
		if(StrKit.isBlank(vo.getStr("id"))){
			bo=new WebsiteContent();
			
			bo.set("id", UUIDKit.getUUID());
			bo.set("removed", "0");
			bo.set("createTime", new Date());
			bo.set("updateTime", new Date());
			bo.set("creator", currUser.getStr("id"));
			bo.set("updator", currUser.getStr("id"));
			bo.set("websiteId", vo.getStr("websiteId"));
			bo.set("websiteColumnId", vo.getStr("websiteColumnId"));
			bo.set("module", vo.getStr("module"));
			bo.set("status", "CMS_CONTENT_STATUS_00");
			bo.set("title", vo.getStr("title"));
			bo.set("attribute", vo.getStr("attribute"));
			bo.set("px", vo.getInt("px"));
			bo.set("pv", 0);
			
			bo.save();
		}else {
			bo=WebsiteContent.dao.findById(vo.getStr("id"));
			
			bo.set("updateTime", new Date());
			bo.set("updator", currUser.getStr("id"));
			bo.set("websiteColumnId", vo.getStr("websiteColumnId"));
			bo.set("title", vo.getStr("title"));
			bo.set("attribute", vo.getStr("attribute"));
			bo.set("px", vo.getInt("px"));
			
			bo.update();
		}
	}
	
	public void changeStatus(String id,String status,Record currUser){
		WebsiteContent bo=WebsiteContent.dao.findById(id);
		bo.set("updateTime", new Date());
		bo.set("updator", currUser.getStr("id"));
		bo.set("status", status);
		bo.update();
	}
	
	public WebsiteContent remove(String id,Record currUser) {
		WebsiteContent bo=WebsiteContent.dao.findById(id);
		bo.set("removed", "1");
		bo.set("updateTime", new Date());
		bo.set("updator", currUser.getStr("id"));
		bo.update();
		return bo;
	}
	
	public WebsiteContent recover(String id,Record currUser) {
		WebsiteContent bo=WebsiteContent.dao.findById(id);
		bo.set("removed", "0");
		bo.set("updateTime", new Date());
		bo.set("updator", currUser.getStr("id"));
		bo.update();
		return bo;
	}
	
	public Record getRecordById(String id){
		String sql="select t.*,w.name websiteName,c.name websiteColumnName,c.code from t_website_content t"
				+ " left join t_website w on t.websiteId=w.id "
				+ " left join t_website_column c on t.websiteColumnId=c.id"
				+ " where t.id=? ";
		Record bo=Db.findFirst(sql, id);
		String[] mapTypes= {"attribute"};
		bo=new ObjectConverter<Record>(bo, null, null, mapTypes).getObject();
		return bo;
	}
}
